import axios from 'axios';
import { Order,DeliveryRecord } from '../types';


// 创建Axios实例并设置基础URL
const api = axios.create({
    // baseURL: import.meta.env.VITE_API_BASE_URL || 'http://192.188.123.2:4399',  
    baseURL: import.meta.env.VITE_API_BASE_URL || 'http://192.168.42.11:4399',  //hgy主机ip
});

// 配送记录
export const recordsApi = {
    // 通过机器人序列号或者处方单号获取配送记录
    getRecords: async (filter: { orderNumber: string | "", serialNumber: string | "" }) => {
        try {
            // console.log('过滤条件 ', filter); // 打印过滤条件
            const response = await api.get('/delivery_record/get', { params: filter });
            // console.log('完整响应数据:', response);  
            console.log('配送记录列表:', response.data.data.recordList);             
            return response.data.data.recordList; // 返回recordList
        } catch (error) {
            console.error('错误 ', error); // 捕获并打印错误信息
            throw error; // 重新抛出错误以便调用者处理
        }
    },
};

//获取未处理订单
export const dispenserApi = {
    getPendingOrders: async () => {
      const {data} = await api.get('/order/unprocessed');
      console.log('未处理订单:', data.data.orderList);
      return data.data.orderList;
    },
};

//系统
export const systemApi = {

  // 系统控制
  startSystem: async (robotCount:number) => {
    const { data } = await api.get('/cmd/start',{params:{robotCount:robotCount}});
    console.log("startssssssssssssssssssssssssssssssssss",data.statusCode);
    return data.statusCode;
  },

  stopSystem: async () => {
    const { data } = await api.get('/cmd/stop',{});
    return data.message;
  },

  //已完成的订单
  getCompletedOrders: async () => {
    const {data} = await api.get('/order/processed');
    // console.log('已完成订单:', data.data.completedOrders);
    return data.data.completedOrders;
  },

  getAliveTime: async () => {
    const {data} = await api.get('/cmd/aliveTime');
    // console.log('运行时间:', data.data.aliveTime);
    return data.data.aliveTime;
  }
};